package com.job.admin.user.service.impl;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.job.admin.model.user.pojo.AdminUser;
import com.job.admin.user.mapper.UserMapper;
import com.job.admin.user.service.UserService;
import com.job.client.common.result.AppHttpCode;
import com.job.client.common.result.ResponseResult;
import com.job.client.common.utils.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public ResponseResult login(AdminUser adminUser) {
        if(adminUser==null || adminUser.getUserNumber()==null || adminUser.getPassword()==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        QueryWrapper<AdminUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_number",adminUser.getUserNumber())
                .eq("password",adminUser.getPassword());
        AdminUser user = userMapper.selectOne(queryWrapper);
        if(user==null){
            return ResponseResult.ErrorResult(AppHttpCode.USERNAME_PASSWORD_ERROR);
        }
        return ResponseResult.OkResult(user);
    }

    @Override
    public ResponseResult getUserInfoByPage(Integer page, Integer size) {
        if(page == null){
            page = 1;
        }
        if(size == null){
            size = 10;
        }
        Page<AdminUser> data = new Page<>(page,size);
        userMapper.selectPage(data,null);
        return ResponseResult.OkResult(data);
    }

    @Override
    public ResponseResult checkUserNumber(String userNumber) {
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_number",userNumber);
        AdminUser user = userMapper.selectOne(queryWrapper);
        if(user == null){
            return ResponseResult.OkResult();
        }
        return ResponseResult.ErrorResult(AppHttpCode.USERNUMBER_EXIST);
    }

    @Override
    public ResponseResult createUser(AdminUser adminUser) {
        if(adminUser == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        adminUser.setUserId(null);
        int i = userMapper.insert(adminUser);
        if(i != 1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult updateUser(AdminUser adminUser) {
        if(adminUser == null || adminUser.getUserId() == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        adminUser.setUpdateTime(DateTimeUtils.createNowTime());
        boolean b = updateById(adminUser);
        if(b){
            return ResponseResult.OkResult();
        }
        return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
    }

    @Override
    public ResponseResult deleteUser(String userId) {
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        int i = userMapper.deleteById(userId);
        if( i != 1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult deleteBluk(List<String> userIds) {
        if(userIds == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        userMapper.deleteBatchIds(userIds);
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult getAllUserInfo() {
        List<AdminUser> list = userMapper.selectList(null);
        return ResponseResult.OkResult(list);
    }

    @Override
    public ResponseResult deletePermission(String userId, String permissionId) {
        if(userId == null || permissionId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        AdminUser user = userMapper.selectById(userId);
        if(user == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_ERROR);
        }
        List<String> permissionList = JSON.parseArray(user.getUserPermission(), String.class);
        permissionList.remove(permissionId);
        user.setUserPermission(JSON.toJSONString(permissionList));
        int i = userMapper.updateById(user);
        if(i != 1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult();
    }

    @Override
    public ResponseResult updatePermission(String userId, List<String> permissionList) {
        if(userId == null || permissionList == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        String jsonString = com.alibaba.fastjson.JSON.toJSONString(permissionList);
        UpdateWrapper<AdminUser>  updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("user_permission",jsonString).eq("user_id",userId);
        int i = userMapper.update(updateWrapper);
        if(i != 1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult();
    }
}
